/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinni.webservicestest;

import com.dinni.webservicestest.dbaccess.User;
import com.dinni.webservicestest.dbaccess.WebServicesDbHelper;
import com.dinni.webservicestest.util.WebServiceHelper;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

/**
 * REST Web Service
 *
 * @author chandrakant
 */
@Path("/user")
public class UserResource {

    @Context
    private UriInfo context;
    @Context
    private Request request;
    private WebServicesDbHelper dbHelper;

    /**
     * Creates a new instance of UserResource
     */
    public UserResource() {
    }
    /*
     * @GET // this method process GET request from client
     * @Produces("text/html") // sends HTML public String
     *
     */

    @GET   // this method process GET request from client    
    @Produces(MediaType.TEXT_HTML)   // sends HTML
    public String getHtml(@QueryParam("username") String username, @QueryParam("password") String password, @QueryParam("debug") boolean debug) {  // username represents the username sent from client                
        User checkedUser = findUser(username, password, debug);
        return WebServiceHelper.htmlHeader + WebServiceHelper.htmlBody
                + WebServiceHelper.formatUserOutputToHtml(checkedUser, username, password)
                + WebServiceHelper.htmlBodyClosingTag + WebServiceHelper.htmlHeaderClosingTag;


    } // end of getHtml

    @GET   // this method process GET request from client    
    @Produces(MediaType.APPLICATION_JSON)   // sends HTML
    public String getJson(@QueryParam("username") String username, @QueryParam("password") String password, @QueryParam("debug") boolean debug) {
       

        User checkedUser = findUser(username, password, debug);
        return WebServiceHelper.formatUserOutputToJson(checkedUser, username, password);


    } // end of geJson

    @GET   // this method process GET request from client    
    @Produces(MediaType.TEXT_XML)   // sends HTML
    public String getXml(@QueryParam("username") String username, @QueryParam("password") String password, @QueryParam("debug") boolean debug) {
        
        
        User checkedUser = findUser(username, password, debug);
        String ret = WebServiceHelper.formatUserOutputToXml(checkedUser, username, password);
        return ret;


    } // end of geJson

    private WebServicesDbHelper getdbHelper() {
        if (dbHelper == null) {
            dbHelper = new WebServicesDbHelper();
        }
        return dbHelper;
    }

    private User findUser(String username, String password, boolean debug) {
        if (username == null || username.trim().equals("") || !WebServiceHelper.isValidEmail(username)) {
            return new User("User", "", "Invalid Email Address For User");
        }
        if (password == null || password.trim().equals("") ) {
            return new User("User", "", "Invalid Password");
        }
        User value;
        if (debug == false) {
            value = (User) WebServiceHelper.userHash.get(username);
            return value;
        }
        //default to getting from db
        value = (User) getUserFromDb(username);
        return value;
    }

    private User getUserFromDb(String uName) {
        User user = getdbHelper().getUserByUserName(uName);
        return user;
    }
    //this is unused

    private List getUsersFromDb() {
        List users = getdbHelper().getUsers();
        return users;
    }
}